//
//  GSSportsDetailsViewController.m
//  GoodManNews
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "GSSportsDetailsViewController.h"
#import "Masonry.h"
#import "GS_SportsDetailsCollectionViewCell.h"
#import "GoodManNews_AFNetTool.h"
#import "GS_SportsItemModel.h"
#import "UIImageView+WebCache.h"

@interface GSSportsDetailsViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UILabel *titleL;
@property (nonatomic, retain) UIView *returnV;
@property (nonatomic, retain) UIButton *returnBut;
@property (nonatomic, retain) UICollectionView *collectionV;
@property (nonatomic, retain) NSMutableArray *imgArr;
@property (nonatomic, copy) NSString *titleStr;
@property (nonatomic, retain) UILabel *nowPageL;
@property (nonatomic, retain) UILabel *partitionL;
@property (nonatomic, retain) UILabel *allPageL;

@end

@implementation GSSportsDetailsViewController

- (void)dealloc
{
    self.collectionV.delegate = nil;
    self.collectionV.dataSource = nil;
    [_allPageL release];
    [_nowPageL release];
    [_partitionL release];
    [_url release];
    [_collectionV release];
    [_returnV release];
    [_imgArr release];
    [_titleStr release];
    [super dealloc];
}

//-(void)viewWillAppear:(BOOL)animated {
//    
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    [self createCollectionView];
    [self getData];
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)createCollectionView
{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    // 设置每个item(cell)的大小
    flowLayout.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height - 20);
    flowLayout.minimumLineSpacing = 0;
    
    // 设置item与四周边界的距离 上左下右
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionV = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    self.collectionV.pagingEnabled = YES;
    self.collectionV.showsHorizontalScrollIndicator = NO;//去掉水平显示的拖拽线
    self.collectionV.backgroundColor = [UIColor blackColor];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    
    [self.collectionV registerClass:[GS_SportsDetailsCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [self.view addSubview:self.collectionV];
    
    [self.collectionV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.view);
        
        
    }];
    
    [flowLayout release];
    [self.collectionV release];
    
    
    
    self.returnV = [UIView new];
    self.returnV.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.returnV];
    
    [self.returnV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.view.mas_bottom).with.offset(-60);
        make.bottom.mas_equalTo(self.view.mas_bottom);
        make.width.mas_equalTo(self.view);
        
    }];
    
    self.returnBut = [UIButton new];
    [self.returnBut setImage:[UIImage imageNamed:@"GS_return"] forState:UIControlStateNormal];
    [self.returnBut addTarget:self action:@selector(butAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.returnV addSubview:self.returnBut];
    [self.returnBut mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.returnV.mas_top).with.offset(5);
        make.bottom.equalTo(self.returnV.mas_bottom).with.offset(-5);
        make.left.equalTo(self.returnV.mas_left).with.offset(20);
        make.width.mas_equalTo(@40);
        
    }];
    
    self.titleL = [UILabel new];
    self.titleL.textColor = [UIColor whiteColor];
    self.titleL.numberOfLines = 0;
//    self.titleL.backgroundColor = [UIColor redColor];
    [self.returnV addSubview:self.titleL];
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.returnBut.mas_right).with.offset(10);
        make.top.equalTo(self.returnV.mas_top).with.offset(5);
        make.bottom.equalTo(self.returnV.mas_bottom).with.offset(-5);
        make.right.equalTo(self.returnV.mas_right).with.offset(-20);
        
    }];
    
    self.nowPageL = [UILabel new];
    self.nowPageL.backgroundColor = [UIColor blackColor];
    self.nowPageL.textAlignment = NSTextAlignmentCenter;
    self.nowPageL.textColor = [UIColor whiteColor];
    self.nowPageL.text = @"1";
    [self.view addSubview:self.nowPageL];
    
    self.partitionL = [UILabel new];
    self.partitionL.backgroundColor = [UIColor blackColor];
    self.partitionL.textAlignment = NSTextAlignmentCenter;
    self.partitionL.textColor = [UIColor whiteColor];
    [self.view addSubview:self.partitionL];
    
    self.allPageL = [UILabel new];
    self.allPageL.backgroundColor = [UIColor blackColor];
    self.allPageL.textAlignment = NSTextAlignmentCenter;
    self.allPageL.textColor = [UIColor whiteColor];
    [self.view addSubview:self.allPageL];
    
    [self.nowPageL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view.mas_centerX).with.offset(-25);
        make.top.equalTo(self.view.mas_bottom).with.offset(-90);
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-70);
        make.width.mas_equalTo(@25);
    }];
    
    [self.partitionL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.equalTo(self.view.mas_bottom).with.offset(-90);
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-70);
        make.width.mas_equalTo(@20);
    }];
    
    [self.allPageL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view.mas_centerX).with.offset(25);
        make.top.equalTo(self.view.mas_bottom).with.offset(-90);
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-70);
        make.width.mas_equalTo(@25);
    }];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imgArr.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *url = self.imgArr[indexPath.row];
    GS_SportsDetailsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blackColor];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
    
    self.titleL.text = self.titleStr;
    self.partitionL.text = @"/";
    self.allPageL.text = [NSString stringWithFormat:@"%ld", self.imgArr.count];
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    // 获取当前的偏移量，计算当前第几页
    NSInteger page = self.collectionV.contentOffset.x / self.collectionV.frame.size.width;
//    NSLog(@"%ld", page);
    self.nowPageL.text = [NSString stringWithFormat:@"%ld", page + 1];
    

    
}

- (void)butAction:(UIButton *)button
{
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)getData
{
    self.imgArr = [NSMutableArray array];
//    NSLog(@"%@", self.url);
    [GoodManNews_AFNetTool getNetWithURL:self.url body:nil headFile:nil responseStyle:WBNJSON success:^(NSURLSessionDataTask *task, id responseObject) {
//        NSLog(@"%@", responseObject);
        
        NSMutableDictionary *bodydic = [responseObject objectForKey:@"body"];
        self.titleStr = [bodydic objectForKey:@"title"];
        
        NSMutableArray *imgArr = [bodydic objectForKey:@"img"];
        
        for (NSMutableDictionary *dic in imgArr) {
            [self.imgArr addObject:[dic objectForKey:@"url"]];
            
        }
//        NSLog(@"%@", self.imgArr);
        [self.collectionV reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
